const express = require('express')
const router = express.Router()
const sqlFn = require('../mysql')
/**
 * @swagger
 * tags:
 *   - name: overview
 *     description: 业务概览
 */

/**
 * @swagger
 * /statistic:
 *   get:
 *     summary: 获取统计数据
 *     description: 获取统计数据的记录
 *     tags: [overview]
 *     responses:
 *       200:
 *         description: 返回统计数据
 */
router.get('/statistic', function (req, res, next) {
    const sql = 'select * from statistics'
    sqlFn(sql, [], function (err, data) {
        if (err) {
            next(err)
            return
        }
        res.send({code: '00000', record: data[0]})
    })
})
/**
 * @swagger
 * /producttypesales:
 *   get:
 *     summary: 获取产品类型销售数据
 *     description: 获取不同产品类型的销售数据
 *     tags: [overview]
 *     responses:
 *       200:
 *         description: 返回产品类型销售数据
 */
router.get('/producttypesales', function (req, res, next) {
    const sql = 'select * from producttypesales'
    sqlFn(sql, [], function (err, data) {
        if (err) {
            next(err)
            return
        }
        res.send({code: '00000', records: data})
    })
})
/**
 * @swagger
 * /mapsales:
 *   get:
 *     summary: 获取地图销售数据
 *     description: 获取基于地图的销售数据
 *     tags: [overview]
 *     responses:
 *       200:
 *         description: 返回地图销售数据
 */
router.get('/mapsales', function (req, res, next) {
    const sql = 'select * from mapsales'
    sqlFn(sql, [], function (err, data) {
        if (err) {
            next(err)
            return
        }
        res.send({code: '00000', records: data})
    })
})
/**
 * @swagger
 * /salestop10:
 *   get:
 *     summary: 获取销售前10的商品
 *     description: 返回销量最高的前10个商品
 *     tags: [overview]
 *     responses:
 *       200:
 *         description: 返回销量前10的数据
 */
router.get('/salestop10', function (req, res, next) {
    const sql = 'select * from salestop10'
    sqlFn(sql, [], function (err, data) {
        if (err) {
            next(err)
            return
        }
        res.send({code: '00000', records: data})
    })
})
/**
 * @swagger
 * /volumetop10:
 *   get:
 *     summary: 获取销售量前10的商品
 *     description: 返回销售量最高的前10个商品
 *     tags: [overview]
 *     responses:
 *       200:
 *         description: 返回销售量前10的数据
 */
router.get('/volumetop10', function (req, res, next) {
    const sql = 'select * from volumetop10'
    sqlFn(sql, [], function (err, data) {
        if (err) {
            next(err)
            return
        }
        res.send({code: '00000', records: data})
    })
})
/**
 * @swagger
 * /salestrend:
 *   get:
 *     summary: 获取销售趋势数据
 *     description: 根据产品类型获取销售趋势数据
 *     tags: [overview]
 *     parameters:
 *       - in: query
 *         name: type
 *         schema:
 *           type: string
 *         required: true
 *         description: 要查询的产品类型
 *     responses:
 *       200:
 *         description: 返回销售趋势数据
 */
router.get('/salestrend', function (req, res, next) {
    const sql = 'select * from salestrend where `type`=?'
    sqlFn(sql, [req.query.type], function (err, data) {
        if (err) {
            next(err)
            return
        }
        res.send({code: '00000', records: data})
    })
})
/**
 * @swagger
 * /hotwords:
 *   get:
 *     summary: 获取热门搜索词
 *     description: 返回最受欢迎的搜索词
 *     tags: [overview]
 *     responses:
 *       200:
 *         description: 返回热门搜索词
 */
router.get('/hotwords', function (req, res, next) {
    const sql = 'select * from hotwords '
    sqlFn(sql, [], function (err, data) {
        if (err) {
            next(err)
            return
        }
        res.send({code: '00000', records: data})
    })
})
module.exports = router